package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.service.IPaperLogService;
import com.shop.admin.validate.PaperLogSearchValidate;
import com.shop.admin.vo.PaperLogListedVo;
import com.shop.admin.vo.PaperLogDetailVo;
import com.shop.common.core.PageResult;
import com.shop.common.entity.paper.PaperLog;
import com.shop.common.mapper.paper.PaperLogMapper;
import com.shop.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 考试记录实现类
 * @author DragonDean
 */
@Service
public class PaperLogServiceImpl implements IPaperLogService {
        
    @Resource
    PaperLogMapper paperLogMapper;

    /**
     * 考试记录列表
     *
     * @author DragonDean
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<PaperLogListedVo>
     */
    @Override
    public PageResult<PaperLogListedVo> list(PageValidate pageValidate, PaperLogSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<PaperLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        paperLogMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userType@user_type:int",
            "like:userName@user_name:str",
            "=:paperId@paper_id:int",
            "like:paperTitle@paper_title:str",
            "=:status:int",
            "=:createIp@create_ip:str",
            "=:userId@user_id:str"
        });

        IPage<PaperLog> iPage = paperLogMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<PaperLogListedVo> list = new LinkedList<>();
        for(PaperLog item : iPage.getRecords()) {
            PaperLogListedVo vo = new PaperLogListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            if(item.getSubmitTime() != null)
                vo.setSubmitTime(TimeUtils.timestampToDate(item.getSubmitTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 考试记录详情
     *
     * @author DragonDean
     * @param id 主键参数
     * @return PaperLog
     */
    @Override
    public PaperLogDetailVo detail(Integer id) {
        PaperLog model = paperLogMapper.selectOne(
                new QueryWrapper<PaperLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        PaperLogDetailVo vo = new PaperLogDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 考试记录删除
     *
     * @author DragonDean
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        PaperLog model = paperLogMapper.selectOne(
                new QueryWrapper<PaperLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        paperLogMapper.delete(new QueryWrapper<PaperLog>().eq("id", id));
    }

}
